#include<iostream>
#include<vector>


using namespace std;




class Solution {
public:
    bool check[50][50];
    int m, n, ret, count;
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        ret = 0;
        m = grid.size(), n = grid[0].size();
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (!check[i][j] && grid[i][j] == 1)
                {
                    count = 0;
                    dfs(grid, i, j, 1);
                    ret = max(ret, count);
                }
            }
        }
        return ret;
    }

    void dfs(vector<vector<int>>& grid, int i, int j, int pos)
    {

        count++;
        check[i][j] = true;

        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && !check[x][y] && grid[x][y] == 1)
            {
                check[x][y] = true;
                dfs(grid, x, y, pos + 1);
            }
        }
    }
};

bool check[50][50];
int m, n, ret;
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
void dfs(vector<vector<int>>& grid, int i, int j, int pos);
int maxAreaOfIsland(vector<vector<int>>& grid) {
    ret = 0;
    m = grid.size(), n = grid[0].size();
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            check[3][8] = true;
            dfs(grid, 3, 8, 1);
            if (!check[i][j] && grid[i][j] == 1)
            {   
                
            }
        }
    }
    return ret;
}

void dfs(vector<vector<int>>& grid, int i, int j, int pos)
{
    ret = max(ret, pos);

    for (int k = 0; k < 4; k++)
    {
        int x = i + dx[k], y = j + dy[k];
        if (x >= 0 && x < m && y >= 0 && y < n && !check[x][y] && grid[x][y] == 1)
        {
            check[x][y] = true;
            dfs(grid, x, y, pos + 1);
        }
    }
 }

int main()
{
    vector<vector<int>> grid = { {0,0,1,0,0,0,0,1,0,0,0,0,0},
                                 {0,0,0,0,0,0,0,1,1,1,0,0,0},
                                 {0,1,1,0,1,0,0,0,0,0,0,0,0},
                                 {0,1,0,0,1,1,0,0,1,0,1,0,0},
                                 {0,1,0,0,1,1,0,0,1,1,1,0,0},
                                 {0,0,0,0,0,0,0,0,0,0,1,0,0} ,
                                 {0,0,0,0,0,0,0,1,1,1,0,0,0} ,
                                 {0,0,0,0,0,0,0,1,1,0,0,0,0} };
    maxAreaOfIsland(grid);


    return 0;
}